/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    adc.c
  * @brief   This file provides code for the configuration
  *          of the ADC instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "hal_adc.h"
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "stdarg.h"
#include "stdlib.h"
#include "elog.h"
#include "cmsis_os.h"

DMA_HandleTypeDef DMA_Init_Handle;

ADC_HandleTypeDef hadc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
{	 
  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */

  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc1.Instance = ADC1;    //选择那个adc                           
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; //6分频100/6
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;           //adc设置为12位
  hadc1.Init.ScanConvMode = DISABLE;                    //关闭扫描模式这里就一个adc
  hadc1.Init.ContinuousConvMode = ENABLE;               //开启连续转换
  hadc1.Init.DiscontinuousConvMode = DISABLE;           //关闭非连续转换
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;  //不设置边沿
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;     //软件触发
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;           //右对齐
  hadc1.Init.NbrOfConversion = 1;                       //一个采集
  hadc1.Init.DMAContinuousRequests = ENABLE;            //开启dma
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;        //eoc标志位
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_1;                    //使用通道一
  sConfig.Rank = 1;                                   //排序第几
  sConfig.SamplingTime = ADC_SAMPLETIME_144CYCLES;    //采样周期
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  { 
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */
 
  /* USER CODE END ADC1_Init 2 */
//HAL_ADC_Start_DMA(&hadc1,buffer1,sizeof(buff));
  
}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */
    /* ADC1 clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**ADC1 GPIO Configuration
    PA1     ------> ADC1_IN1
    */
    GPIO_InitStruct.Pin = GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	__HAL_RCC_DMA2_CLK_ENABLE();
    DMA_Init_Handle.Instance = DMA2_Stream0;                              //DMA类似于横纵坐标
    DMA_Init_Handle.Init.Channel = DMA_CHANNEL_0;                         //DMA类似于横纵坐标详细看手册
    DMA_Init_Handle.Init.Direction = DMA_PERIPH_TO_MEMORY; 			      //外设到内存
	DMA_Init_Handle.Init.PeriphInc = DMA_PINC_DISABLE;     			   	  //不递增地址
    DMA_Init_Handle.Init.MemInc = DMA_MINC_DISABLE;         		      //不递增地址
    DMA_Init_Handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;   //半字节
    DMA_Init_Handle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;      //半字节
    DMA_Init_Handle.Init.Mode = DMA_CIRCULAR;                             //循环转换
    DMA_Init_Handle.Init.Priority = DMA_PRIORITY_HIGH;                    //速度
    DMA_Init_Handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;                 
	HAL_DMA_Init(&DMA_Init_Handle);                                       //初始化dma
	//这里直接强硬调用底层接口，如果使用封装函数直接去读DR寄存器会清楚EOC标志位
	 __HAL_LINKDMA(&hadc1,DMA_Handle,DMA_Init_Handle);	                  //链接adc总控结构体
	  
    HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 5, 0);                        //优先级
    HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);                                //优先级  
//  HAL_NVIC_SetPriority(ADC_IRQn,3,0);   
//	HAL_NVIC_EnableIRQ(ADC_IRQn);        
  
  }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspDeInit 0 */

  /* USER CODE END ADC1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_ADC1_CLK_DISABLE();

    /**ADC1 GPIO Configuration
    PA1     ------> ADC1_IN1
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1);

  /* USER CODE BEGIN ADC1_MspDeInit 1 */

  /* USER CODE END ADC1_MspDeInit 1 */
  }
}




